package com.iot.ic.controller;


import com.iot.ic.base.BaseController;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.Location;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.TeamLocation;
import com.iot.ic.service.LocationService;
import com.iot.ic.service.TeamLocationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by xiuge on 2017/7/10.
 */
@RestController
@Api(description = "车队位置信息相关接口")
public class TeamLocationControlle extends BaseController{

    @Autowired
    private TeamLocationService teamLocationService;
	@Autowired
	SimpMessagingTemplate simpMessagingTemplate;

	@ApiOperation(value="添加定位信息信息", notes="")
	@ResponseBody
    @PostMapping("/teamLocations")
    public Response uploadLocation(@Valid@RequestBody TeamLocation teamLocation){
		Response response=new Response();
		teamLocation.setDelFlag(0);
		teamLocation.setLocationTime(new Date());
        int result=teamLocationService.addLocationHistory(teamLocation);
        if (result == 1) {
			response.setInfos(ReturnCode.No_Err);
		} else {
			response.setInfos(ReturnCode.Err_SaveLocationFailor);
		}
		return response;
    }
	
	/**
	 * 查询车队位置信息
	 * @param userId 用户ID
	 * @return
	 */
	@ApiOperation(value="查询车队位置信息", notes="")
	@ResponseBody
	@RequestMapping(value = "/user/{userId}/team/locations", method = RequestMethod.GET)
	public Response listDeviceLocation (@PathVariable("userId") int adminId, @RequestParam(value="teamState",required= false) Integer teamState) {
		Response response=new Response();
	    TeamLocation teamLocation = new TeamLocation();
	    teamLocation.setTeamState(teamState);
	    List<TeamLocation> teamLocations = teamLocationService.listTeamLocations(teamLocation);
		response.setResult(teamLocations);
	
		return response;
	}

	@ApiOperation(value="查询车队位置信息，websocket请求", notes="")
	@MessageMapping("/user/team/locations")
	@SendToUser("/user/teamLocations")
	public void listDeviceLocationSocket (
			@Header("userId") String adminId,
			@Header(value="teamState",required= false) Integer teamState)
	{
		Response response=new Response();
		TeamLocation teamLocation = new TeamLocation();
		teamLocation.setTeamState(teamState);
		List<TeamLocation> teamLocations = teamLocationService.listTeamLocations(teamLocation);
		response.setResult(teamLocations);

		simpMessagingTemplate.convertAndSendToUser(adminId, "/userTeamLocations", response);
	}

}
